﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GoobizFrame.Windows.Forms;

namespace GoobizFrame.Windows.Controls
{
    /// <summary>
    /// XtraButtonEditor: Mãng các bDevExpress.XtraEditors.SimpleButtonutton 
    /// <br>- Utility: thay thế toolbar trong FormUpdateWithToolbar</br>
    /// <br>- Owner: Phuongphan</br>
    /// </summary>
    public partial class XtraButtonEditor : DevExpress.XtraEditors.XtraUserControl
    {
        #region local variable
        bool enableAdd = true;
        bool enableEdit = true;
        bool enableDelete = true;
        bool enablePrintPreview = true;
        bool enableTest = true;
        bool enableVerify = true;
        bool enableQuery = true;
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        public XtraButtonEditor()
        {
            InitializeComponent();
        }

        #region FormUpdateWithToolbar
        private Forms.FormUpdateWithToolbar frmUpdateWithToolbar;
        [Category("XtraButtonEditor")]
        [Description("...")]
        public Forms.FormUpdateWithToolbar FrmUpdateWithToolbar
        {
            get { return frmUpdateWithToolbar; }
            set
            {
                frmUpdateWithToolbar = value;
                if (value != null)
                {
                    frmUpdateWithToolbar.AfterCheckUserRightAction += new EventHandler(frmUpdateWithToolbar_AfterCheckUserRightAction);
                    frmUpdateWithToolbar.Load += new EventHandler(frmUpdateWithToolbar_Load);
                    frmUpdateWithToolbar.AfterChangeFormState += new GoobizFrame.Windows.Forms.FormStateEventHandler(frmUpdateWithToolbar_AfterChangeFormState);
                }
            }
        }

        private bool useToolbarMethod = true;
        [Category("XtraButtonEditor")]
        [Description("...")]
        [DefaultValue(true)]
        public bool UseToolbarMethod
        {
            get { return useToolbarMethod; }
            set { useToolbarMethod = value; }
        }


        /// <summary>
        /// Sự kiện sau khi thay đổi trạng thái các item trên toolbar của FormUpdateWithToolbar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmUpdateWithToolbar_AfterChangeFormState(object sender, GoobizFrame.Windows.Forms.FormStateEventArgs e)
        {
            ChangeFormState(e.NewFormState);
        }

        /// <summary>
        /// Sự kiện load FormUpdateWithToolbar 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmUpdateWithToolbar_Load(object sender, EventArgs e)
        {
            ChangeFormState(GoobizFrame.Windows.Forms.FormState.View);
        }

        /// <summary>
        /// Sự kiện sau khi kiểm tra quyền thao tác trên FormUpdateWithToolbar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmUpdateWithToolbar_AfterCheckUserRightAction(object sender, EventArgs e)
        {
            enableAdd = frmUpdateWithToolbar.EnableAdd;
            enableEdit = frmUpdateWithToolbar.EnableEdit;
            enableDelete = frmUpdateWithToolbar.EnableDelete;

            enableQuery = frmUpdateWithToolbar.EnableQuery;
            enablePrintPreview = frmUpdateWithToolbar.EnablePrintPreview;

            enableTest = frmUpdateWithToolbar.EnableTest;
            enableVerify = frmUpdateWithToolbar.EnableVerify;
        }

        /// <summary>
        /// Sự kiện đóng FormUpdateWithToolbar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmUpdateWithToolbar_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ClickClose != null)
                ClickClose(sender, e);
        }

        #endregion

        /// <summary>
        /// Thay đổi trạng thái form ứng với các thao tác cập nhật dữ liệu
        /// </summary>
        /// <param name="state"></param>
        public void ChangeFormState(Forms.FormState state)
        {
            switch (state)
            {
                case Forms.FormState.Save:
                case Forms.FormState.Cancel:
                case Forms.FormState.View:
                    Item_Save.Enabled = false;
                    Item_Cancel.Enabled = false;
                    Item_Add.Enabled = enableAdd;
                    Item_Edit.Enabled = enableEdit;
                    Item_Delete.Enabled = enableDelete;
                    Item_Close.Enabled = true;
                    Item_PrintPreview.Enabled = enablePrintPreview;
                    Item_Query.Enabled = enableQuery;
                    //item_Select.Enabled = true;
                    Item_Refresh.Enabled = true;
                    Item_Test.Enabled = enableTest;
                    Item_Verify.Enabled = enableVerify;
                    break;
                case Forms.FormState.Add:
                case Forms.FormState.Edit:
                    Item_Save.Enabled = true;
                    Item_Cancel.Enabled = true;
                    Item_Add.Enabled = false;
                    Item_Edit.Enabled = false;
                    Item_Delete.Enabled = false;
                    Item_Close.Enabled = false;
                    Item_PrintPreview.Enabled = false;
                    Item_Query.Enabled = false;
                    //item_Select.Enabled = false;
                    Item_Refresh.Enabled = false;
                    Item_Test.Enabled = false;
                    Item_Verify.Enabled = false;
                    break;

            }
        }

        #region events
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickAdd;
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickEdit;
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickDelete;
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickSave;
       
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickCancel;
       
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickQuery;
  
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickPrint;
        
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickClose;
       
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickRefresh;
  
        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickTest;

        [Category("XtraButtonEditor")]
        [Description("...")]
        public event EventHandler ClickVerify;

        #endregion

        #region Buttons

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonAdd
        {
            get { return Item_Add; }
            set { Item_Add = value; }
        }
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonEdit
        {
            get { return Item_Edit; }
            set { Item_Edit = value; }
        }
      
        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonDelete
        {
            get { return Item_Delete; }
            set { Item_Delete = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonSave
        {
            get { return Item_Save; }
            set { Item_Save = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonCancel
        {
            get { return Item_Cancel; }
            set { Item_Cancel = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonPrintPreview
        {
            get { return Item_PrintPreview; }
            set { Item_PrintPreview = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonClose
        {
            get { return Item_Close; }
            set { Item_Close = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonRefresh
        {
            get { return Item_Refresh; }
            set { Item_Refresh = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonTest
        {
            get { return Item_Test; }
            set { Item_Test = value; }
        }

        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonVerify
        {
            get { return Item_Verify; }
            set { Item_Verify = value; }
        } 
 
        [Category("XtraButtonEditor")]
        [Description("...")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),Browsable(true)]
        public DevExpress.XtraEditors.SimpleButton ButtonQuery
        {
            get { return Item_Query; }
            set { Item_Query = value; }
        } 

        #endregion 

        #region Raise Event Handler
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                if (frmUpdateWithToolbar.EnableAdd)
                {
                    bool valid = true;
                    if (UseToolbarMethod)
                        valid = frmUpdateWithToolbar.PerformAdd();
                    if (valid) this.frmUpdateWithToolbar.ChangeFormState(GoobizFrame.Windows.Forms.FormState.Add);
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            }
            if (ClickAdd != null)
                ClickAdd(sender, e);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                if (frmUpdateWithToolbar.EnableEdit)
                {
                    bool valid = true;
                    if (UseToolbarMethod)
                        valid = frmUpdateWithToolbar.PerformEdit();
                    if (valid) this.frmUpdateWithToolbar.ChangeFormState(GoobizFrame.Windows.Forms.FormState.Edit);
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            }
            if (ClickEdit != null)
                ClickEdit(sender, e);

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                if (frmUpdateWithToolbar.EnableDelete)
                {
                    bool valid = true;
                    if (UseToolbarMethod)
                        valid = frmUpdateWithToolbar.PerformDelete();
                    if (valid) this.frmUpdateWithToolbar.ChangeFormState(GoobizFrame.Windows.Forms.FormState.View);
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            }
            if (ClickDelete != null)
                ClickDelete(sender, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                bool valid = true;
                if (UseToolbarMethod)
                    valid = frmUpdateWithToolbar.PerformSave();
                if (valid) frmUpdateWithToolbar.ChangeFormState(GoobizFrame.Windows.Forms.FormState.View);
            }
            if (ClickSave != null)
                ClickSave(sender,e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                bool valid = true;
                if (UseToolbarMethod)
                    valid = frmUpdateWithToolbar.PerformCancel();
                if (valid) this.frmUpdateWithToolbar.ChangeFormState(GoobizFrame.Windows.Forms.FormState.View);
            }
            if (ClickCancel != null)
                ClickCancel(sender, e);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null && UseToolbarMethod)
                if (frmUpdateWithToolbar.EnablePrintPreview)
                {
                    frmUpdateWithToolbar.PerformPrintPreview();
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            if (ClickPrint != null)
                ClickPrint(sender, e);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null)
            {
                frmUpdateWithToolbar.FormClosing += new FormClosingEventHandler(frmUpdateWithToolbar_FormClosing);
                this.frmUpdateWithToolbar.Close();
            }
           
        }

        private void Item_Refresh_Click(object sender, EventArgs e)
        {
            if (ClickRefresh != null)
                ClickRefresh(sender, e);

        }

        private void Item_Test_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null && UseToolbarMethod)
                if (frmUpdateWithToolbar.EnableTest)
                {
                    frmUpdateWithToolbar.PerformTest();
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            if (ClickTest != null)
                ClickTest(sender, e);

        }

        private void Item_Verify_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null && UseToolbarMethod)
                if (frmUpdateWithToolbar.EnableVerify)
                {
                    frmUpdateWithToolbar.PerformVerify();
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            if (ClickVerify != null)
                ClickVerify(sender, e);

        }

        private void Item_Query_Click(object sender, EventArgs e)
        {
            if (frmUpdateWithToolbar != null && UseToolbarMethod)
                if (frmUpdateWithToolbar.EnableQuery)
                {
                    frmUpdateWithToolbar.PerformQuery();
                }
                else
                    UserMessage.Show("SYS_ILLEGAL_ACTION", new string[] { });
            if (ClickQuery != null)
                ClickQuery(sender, e);
        }

        #endregion


       

   
       

        

      
    }
}
